-- SPDX-License-Identifier: GPL-3.0-or-later


local VslUtil = {}

-- local red = "\27[31m"
-- local reset = "\27[0m"

--狂野模式开关
--记得把zzsystem.lua里的也设置为true
-- VslUtil.wild_mode = false --狂野模式！！（仅限服务器用，解放偷梁换柱、猴子）

--------------------------------------------------
--Vsl包名
--好吧这疑似没什么用，后续再考虑怎么处理改名问题
--------------------------------------------------

-- VslUtil.extensionName = "VirtualSlash"
VslUtil.extensionName = "VslTest"

--------------------------------------------------
--命名大全
--------------------------------------------------



--------------------------------------------------
--combobox大全
--------------------------------------------------

VslUtil.suits = {
   "heart", "diamond", "spade", "club",
}

--------------------------------------------------
--标签显示大全
--涉及花色的部分，如何改良后续参考谋黄忠烈弓
--------------------------------------------------

VslUtil.short_card_type = {
  basic = "基",
  trick = "锦",
  equip = "装",
}

VslUtil.card_type = {
  basic = "基本牌",
  trick = "锦囊牌",
  equip = "装备牌",
}

VslUtil.sub_card_type = {
  delay_trick = "延时锦囊牌",
}

VslUtil.area_type = {
  hand = "手牌区",
  equip = "装备区",
  judge = "判定区",
}

VslUtil.card_suits_table = {
  spade = "♠",
  club = "♣",
  heart = "<font color='red'>♥</font>",
  diamond = "<font color='red'>♦</font>",
}

--------------------------------------------------
--三目运算符
--------------------------------------------------

VslUtil.Ternary = function(A, B, C)
	if A then
		return B
	else
		return C
	end
end

--- 检查玩家是否存在对应阶段
VslUtil.exist_or_not = function(player, Phase)
  local exist_truth = true
  if player.skipped_phases ~= {} then
      for k in ipairs(player.skipped_phases) do
        if k == Phase then
            exist_truth = false
        end
      end
  end
  return exist_truth
end

--- 检查是否不能使用、打出或弃置某种牌
---@param player ServerPlayer
---@param card Card
---@return boolean
VslUtil.is_jilei = function(player, card)
  local is_jilei_or_not = false
  -- if not player:canUse(card) or player:prohibitUse(card) or player:prohibitDiscard(card) or player:prohibitResponse(card) then
  if player:prohibitUse(card) or player:prohibitDiscard(card) or player:prohibitResponse(card) then
    is_jilei_or_not = true
  end
  return is_jilei_or_not
end

--- 检查是否受伤（不涉及回复的话用这个）
---@param player Player
---@return boolean
VslUtil.is_wounded = function(player)
  -- local chain_wounded = false
  -- for _,p in ipairs(Fk:currentRoom().alive_players) do
  -- -- for _,p in ipairs(room.alive_players) do
  -- -- for _,p in ipairs(Fk:currentRoom():getAlivePlayers()) do
  --   -- if p:getMark("v_wounded_chained") > 0 then
  --   if p:hasSkill("v_bingfu") then  
  --     chain_wounded = true
  --   end
  -- end
  -- if chain_wounded then
  --   return player.chained or player:isWounded()
  -- else
  --   return player:isWounded()
  -- end
  return player:isWounded()
end

--判断敌我
VslUtil.isFriend = function(ai, target)
    local room = target.room
    if room.settings.gameMode == "v_m_2v2_mode" then
        --后续要改的，因为哪怕是明牌打牌，我们这个模式暂定身份都是特制的
        return ai:isFriend(target)
    else
        return ai:isFriend(target)
    end
end

VslUtil.isEnemy = function(ai, target)
    local room = target.room
    return not ai:isFriend(target)
end

--判断是否能被技能指定为目标
--注意与ai同步

VslUtil.SkillCanTarget = function(to, player, skill_name)
  return true
end


--回合弃牌区
---@param room Room
---@return integer[]
VslUtil.getCurrentCenterzone = function(room)
  local ids = {}
  if not room.current or room.current.dead then return {} end
  local logic = room.logic
  local events = logic.event_recorder[GameEvent.MoveCards] or Util.DummyTable
  if not logic:getCurrentEvent():findParent(GameEvent.Turn, true) then return {} end
  local end_id = logic:getCurrentEvent():findParent(GameEvent.Turn, true).id
  for i = #events, 1, -1 do
    local e = events[i]
    if e.id < end_id then break end
    for _, move in ipairs(e.data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          table.insertIfNeed(ids, info.cardId)
        end
      end
    end
  end
  local cards = table.simpleClone(ids)
  for _, id in ipairs(cards) do
    if room:getCardArea(id) ~= Card.DiscardPile then
      table.removeOne(ids, id)
    end
  end
  local ignores = room.current:getMark("ignore_centerzone-turn")
  if type(ignores) == "table" and #ignores > 0 then
    for _, id in ipairs(ignores) do
      if room:getCardArea(id) == Card.DiscardPile then
        table.removeOne(ids, id)
      end
    end
  end
  return ids
end

--移去
---@param room Room
---@param ids integer[] | nil
VslUtil.ignoreCenterzone = function(room, ids)
  local ignores = room.current:getMark("ignore_centerzone-turn")
  if type(ignores) ~= "table" then ignores = {} end
  if type(ids) == "table" and #ids > 0 then
    for _, id in ipairs(ids) do
      room:sendLog{
        type = "#ignoreCenterzoneCard",
        arg = Fk:getCardById(id, true):toLogString(),
      }
    end
    table.insertTable(ignores, ids)
  end
  room:setPlayerMark(room.current, "ignore_centerzone-turn", ignores)
end

--- 判断花色相近
---@param suit Suit
---@return Suit
VslUtil.suit_close = function(suit)
    if suit == Card.Spade then
        return Card.Club
    elseif suit == Card.Club then
        return Card.Spade
    elseif suit == Card.Heart then
        return Card.Diamond
    elseif suit == Card.Diamond then
        return Card.Heart
    end
    return Card.NoSuit
end
  
--- 判断花色相对
---@param suit Suit
---@return Suit
VslUtil.suit_opposite = function(suit)
    if suit == Card.Spade then
        return Card.Heart
    elseif suit == Card.Club then
        return Card.Diamond
    elseif suit == Card.Heart then
        return Card.Spade
    elseif suit == Card.Diamond then
        return Card.Club
    end
    return Card.NoSuit
end

--- 返回花色数字对应的字段
---@param suit Suit
---@return string
VslUtil.suit_string = function(suit)
    if suit == Card.Club then
        return "club"
    elseif suit == Card.Spade then
        return "spade"
    elseif suit == Card.Diamond then
        return "diamond"
    elseif suit == Card.Heart then
        return "heart"
    end
    return "unknown"
end

--- 返回花色数字对应的字段
---@param s string
---@return Suit
VslUtil.suit_number = function(s)
  if s == "club" then
    return Card.Club
  elseif s == "spade" then
    return Card.Spade
  elseif s == "diamond" then
    return Card.Diamond
  elseif s == "heart" then
    return Card.Heart
  end
end

---返回phase对应的字符串
---请注意8 notactive、9 phasenone暂无官方叫法，暂未写入
---@param phase_int integer
---@return string
VslUtil.phase_string = function(phase_int)
  if phase_int == 1 then
    return "回合开始"
  elseif phase_int == 2 then
    return "准备"
  elseif phase_int == 3 then
    return "判定"
  elseif phase_int == 4 then
    return "摸牌"
  elseif phase_int == 5 then
    return "出牌"
  elseif phase_int == 6 then
    return "弃牌"
  elseif phase_int == 7 then
    return "结束"
  elseif phase_int == 8 then
    return "回合外"
  elseif phase_int == 9 then
    return "空白"
  end
  return "未知"
end

---@param suit string
---@return string
VslUtil.card_suits_table = function(suit)
  if suit == "spade" then
      return "♠"
    elseif suit == "club" then
      return "♣"
    elseif suit == "heart" then
      return "<font color='red'>♥</font>"
    elseif suit == "diamond" then
      return "<font color='red'>♦</font>"
  end
  return "unknown"
end

--------------------------------------------------
--以下原型为官方Util包的内容，现更新至0.5.12版
--可能会进行本土化改造（例如鬼勇）
--------------------------------------------------

return VslUtil